package cn.demo.app.a.controller;

import cn.demo.app.a.domain.User;
import cn.demo.app.a.service.UserService;
import cn.ifloat.brick.basic.common.toolkit.StringUtils;
import cn.ifloat.brick.logger.annotations.OperationLog;
import cn.ifloat.brick.logger.annotations.PerformanceLog;
import cn.ifloat.brick.logger.common.PerformanceUnit;
import cn.ifloat.brick.redis.core.RedissonClientCore;
import cn.ifloat.brick.redis.operations.struct.locks.RedisLock;
import cn.ifloat.brick.sprofile.common.init.config.SpringUtil;
import cn.ifloat.brick.sprofile.redis.init.RedisClientContext;
import cn.ifloat.brick.sprofile.web.common.BasicController;
import cn.ifloat.brick.sprofile.web.init.config.WebEnvironmentConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/axios")
public class AxiosController extends BasicController {
    @Autowired
    private UserService userService;

    @Autowired
    private WebEnvironmentConfig webEnvironmentConfig;

    private UserService copyUserService;

    @Autowired
    private RedisClientContext redisClientContext;

    @PostConstruct
    public void init() {
        copyUserService = SpringUtil.getBean(UserService.class);
        logger.info("init method success");
    }

//    @RequestMapping("layout")
//    @PerformanceLog(unit = PerformanceUnit.MILLS)
//    @OperationLog(logTlt = "操作日志测试 ${result} : ${now} ", application = "example", service = "user", condition = "")
//    public String aaa(Model model) {
//        List<User> list = userService.list();
//        model.addAttribute("items", list);
//        return "user/axios";
//    }

    /**
     * 查看用户列表
     *
     * @param user 条件
     * @return 结果
     */
    @GetMapping("/list")
    public List<User> list(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //name不为空时，组装模糊查询条件
        wrapper.like(StringUtils.isNotBlank(user.getName()), "name", user.getName());
        wrapper.eq(!Objects.equals(user.getId(), null), "id", user.getId());
        List<User> list = userService.list(wrapper);
        return list;
    }

    /**
     * 查看用户详情
     * @param id 用户ID
     * @return 结果
     */
    @GetMapping("/info")
    public User getInfo(Long id) {
        return userService.getById(id);
    }

    /**
     * 删除用户
     * @param id 用户ID
     * @return 结果
     */
    @DeleteMapping("/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return userService.removeById(id);
    }

    /**
     * 修改用户信息
     * @param user 内容
     * @return 结果
     */
    @PutMapping("/edit")
    public Boolean edit(@RequestBody User user) {
     return userService.updateById(user);
    }

    /**
     * 新增用户信息
     * @param user  用户信息
     * @return 结果
     */
    @PostMapping("/add")
    public void add(@RequestBody User user) {
        user.setCreateTime(new Date());
        userService.save(user);
    }

}

